package com.ruoyi.web.controller.system;

import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.system.domain.SysCarStateInfo;
import com.ruoyi.system.domain.SysOwnerCars;
import com.ruoyi.system.service.ISysCarStateInfoService;
import com.ruoyi.system.service.ISysOwnerCarsService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.SysCars;
import com.ruoyi.system.service.ISysCarsService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 车辆信息表Controller
 * 
 * @author ruoyi
 * @date 2022-05-11
 */
@RestController
@RequestMapping("/system/cars")
public class SysCarsController extends BaseController
{
    @Autowired
    private ISysCarsService sysCarsService;
    @Autowired
    private ISysCarStateInfoService sysCarStateInfoService;
    @Autowired
    private ISysOwnerCarsService sysOwnerCarsService;

    /**
     * 查询车辆信息表列表
     */
    @GetMapping("/list")
    public TableDataInfo list(SysCars sysCars)
    {
        if (!"admin".equals(getUsername())) {
            Long userId = getUserId();
            SysOwnerCars sysOwnerCars = new SysOwnerCars();
            sysOwnerCars.setOwnerId(userId);
            List<SysOwnerCars> ownerCars = sysOwnerCarsService.selectSysOwnerCarsList(sysOwnerCars);
            if (ownerCars == null || ownerCars.size() == 0) return null;
            Long[] ids = ownerCars.stream().map(SysOwnerCars::getCarId).toArray(Long[]::new);
            startPage();
            List<SysCars> list = sysCarsService.selectSysCarsListByIds(ids);
            return getDataTable(list);
        }
        startPage();
        List<SysCars> list = sysCarsService.selectSysCarsList(sysCars);
        return getDataTable(list);
    }

    /**
     * 导出车辆信息表列表
     */
    @Log(title = "车辆信息表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysCars sysCars)
    {
        List<SysCars> list = sysCarsService.selectSysCarsList(sysCars);
        ExcelUtil<SysCars> util = new ExcelUtil<SysCars>(SysCars.class);
        util.exportExcel(response, list, "车辆信息表数据");
    }

    /**
     * 获取车辆信息表详细信息
     */
    @GetMapping(value = "/{carId}")
    public AjaxResult getInfo(@PathVariable("carId") Long carId)
    {
        return AjaxResult.success(sysCarsService.selectSysCarsByCarId(carId));
    }

    /**
     * 获取车辆信息表详细信息
     */
    @GetMapping(value = "/stateId/{stateInfoId}")
    public AjaxResult getCarInfo(@PathVariable("stateInfoId") Long stateInfoId)
    {
        SysCarStateInfo sysCarStateInfo = sysCarStateInfoService.selectSysCarStateInfoByInfoId(stateInfoId);
        if (sysCarStateInfo == null) return error("无对应车信息！");
        return AjaxResult.success(sysCarsService.selectSysCarsByCarId(sysCarStateInfo.getCarId()));
    }

    /**
     * 新增车辆信息表
     */
    @Log(title = "车辆信息表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody SysCars sysCars)
    {
        if (sysCars == null) return error("空数据！");
        return toAjax(sysCarsService.insertSysCars(sysCars, getUserId()));
    }

    /**
     * 修改车辆信息表
     */
    @Log(title = "车辆信息表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody SysCars sysCars)
    {
        return toAjax(sysCarsService.updateSysCars(sysCars));
    }

    /**
     * 删除车辆信息表
     */
    @Log(title = "车辆信息表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{carIds}")
    public AjaxResult remove(@PathVariable Long[] carIds)
    {
        return toAjax(sysCarsService.deleteSysCarsByCarIds(carIds));
    }
}
